# -*- encoding: utf-8 -*-
"""
@File    : Thread.py
@Time    : 2021/5/20 11:05
@Author  : fkl
@Software: PyCharm
"""

import threading
import requests
import parsel
import time


class MyThread(threading.Thread):
    """
    多线程
    """

    def __init__(self, url, headers):
        threading.Thread.__init__(self)
        self.status = 0
        self.url = url
        self.headers = headers
        self.title = None
        self.content = ''
        self.backtime = 3

    def run(self):
        try:
            response3 = requests.get(self.url, self.headers)
            selector3 = parsel.Selector(text=response3.text)
            con = selector3.xpath('//div[@id="book"]/div[@class="content"]')
            self.title = con.xpath('//h1/text()').get()
            content = con.xpath('//div[@id="content"]/text()').extract()
            for v2 in content:
                self.content += v2 + "\n"
            self.status = 1

        except Exception as e:
            time.sleep(1000)
            self.backtime = self.backtime - 1
            if self.backtime > 0:
                self.run()
